﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Utility.Json;
using System.Data;
using Hxj.Data;
using Utility;
using Utility.Model;

namespace MsgCenter.Web.Handlers
{
    /// <summary>
    /// Handler_Dic 的摘要说明
    /// </summary>
    public class Handler_Dic : BaseUI.BaseHandler
    {
        /// <summary>
        /// 删除字典或字典值
        /// </summary>
        public void Delete()
        {
            try
            {
                string strID = ParamsOfRequest.Request("DeptID");
                string strOptionType = ParamsOfRequest.Request("OptionType");
               BLL.BLLServiceHelper.GetDicService().DeleteDic(strID);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }
        }
        /// <summary>
        /// 获取下级个数
        /// </summary>
        public void GetChildrenCount()
        {
            string strOptionType = ParamsOfRequest.Request("OptionType");
            string strFlag = "0";
            if (strOptionType == "Dic")
            {
                WhereClip wcp = new WhereClip();
                wcp = wcp.And(Sys_Dic._.ParentDicID == ParamsOfRequest.Request("DeptID"));
                wcp = wcp.And(Sys_Dic._.IsDelete == false);
                using (DataTable dt =BLL.BLLServiceHelper.GetDicService().GetDicTable(wcp))
                {
                    strFlag = dt.Rows.Count.ToIntString();
                }
            }
            base.Response.Write(strFlag);
        }
        /// <summary>
        /// 创建字典树形
        /// </summary>
        public void CreateDicTree()
        {
            TreeItem ti;
            string strEmp = Guid.Empty.ToString();
            ti = new TreeItem() { id = "0", text = "字典维护列表", value = "One", isexpand = true, showcheck = false };
            WhereClip wcp = new WhereClip();
            using (DataTable dtDicTable =BLL.BLLServiceHelper.GetDicService().GetDicTable(wcp))
            {
                GetTreeModuleItemFromDataTable(ti, dtDicTable, Guid.Empty);
                string strResult = ti.ToJsonString();
                base.Response.Write(strResult);
            }
        }

        private void GetTreeModuleItemFromDataTable(TreeItem ti, DataTable dtDic, Guid sId)
        {
            CreateOne(ti, sId, dtDic);
        }

        private void CreateOne(TreeItem ti, Guid sId, DataTable dtDic)
        {
            TreeItem ti0;
            string parent = string.Empty;
            ti.ChildNodes = new List<TreeItem>();
            DataRow[] drs0 = dtDic.Select("ParentDicID='" + sId + "'", "Sort");
            string strDicName = string.Empty;
            foreach (DataRow dr in drs0)
            {
                strDicName = dr["DicName"].ToString();
                parent = dr["DicID"].ToString();
                if (dr["IsDelete"].ToSafeString().ToSafeBool())
                {
                    ti0 = new TreeItem() { id = dr["DicID"].ToString(), value = "Dic", text = strDicName, showcheck = false, isexpand = false, isdelete = true, deletetext = "已删除" };
                }
                else
                {
                    ti0 = new TreeItem() { id = dr["DicID"].ToString(), value = "Dic", text = strDicName, showcheck = false, isexpand = false };
                }
                CreateNode(ti0, parent, dtDic);
                ti.Add(ti0);
            }
        }
        private void CreateNode(TreeItem ti, string sId, DataTable dtDic)
        {
            TreeItem ti0;
            string parent = string.Empty;
            ti.ChildNodes = new List<TreeItem>();
            DataRow[] drs0 = dtDic.Select("ParentDicID='" + sId + "'", "Sort");
            string strDicName = string.Empty;
            foreach (DataRow dr in drs0)
            {
                strDicName = dr["DicName"].ToString();
                parent = dr["DicID"].ToString();
                if (dr["IsDelete"].ToSafeString().ToSafeBool())
                {
                    ti0 = new TreeItem() { id = dr["DicID"].ToString(), value = "DicValue", text = strDicName, showcheck = false, isexpand = false, isdelete = true, deletetext = "已删除" };
                }
                else
                {
                    ti0 = new TreeItem() { id = dr["DicID"].ToString(), value = "DicValue", text = strDicName, showcheck = false, isexpand = false };
                }
                ti.Add(ti0);
            }
        }

    }
}